<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/15 0015
 * Time: 14:29
 */

namespace backend\controllers;


use backend\components\BaseComponentController;
use common\components\DataHelper;
use common\models\Percentage\Percentage;
use common\models\PercentageItem\PercentageItem;
use common\models\Plug\Plug;
use common\models\PlugsRunTotal\PlugsRunTotal;
use yii\helpers\Url;

class PercentageController extends BaseComponentController
{
    public function actionIndex()
    {
        $plugs = Plug::find()->select(["id", "name"])->asArray()->all();
        if ( ($date = $this->get("date_view")) ) {
            if ( !($percentage = Percentage::set($date, $this->get("plug_id_view"), $this->get("upper_terminal_view", 0))) ) {
                return $this->render("index", [
                    "error" => Percentage::error(),
                    "plugs" => $plugs
                ]);
            }
            if ( $percentage->status == Percentage::STATUS_NO ) {
                $ret = PercentageItem::set($percentage, true);
                $ret = DataHelper::array_sort($ret, "upper_num", "desc");
            } else {
                $ret = PercentageItem::find()->where([
                    "percentage_id" => $percentage->id,
                    "parent_agent" => 0
                ])->with(["agentInfo", "managerInfo"])->orderBy("upper_num desc")->asArray()->all();
                foreach ( $ret as &$item ) {
                    $item["agent_name"] = $item["agentInfo"]["name"];
                    $item["manager_name"] = $item["managerInfo"]["name"];
                    unset($item["agent"]);
                    unset($item["manager"]);
                }
            }

            $totalRet = PlugsRunTotal::find()->where([
                "plug_id" => $this->get("plug_id_view"),
                "created_date" => $date,
            ])->select([
                "sum(terminal_num) as totalNum"
            ])->asArray()->one();
            $totalRet = intval($totalRet["totalNum"]);/*
            $i = 0;
            foreach ( $ret as $item ) {
                $i += $item["upper_num"];
            }
            die(var_dump($i));*/
            return $this->render("index", [
                "ret" => $ret,
                "percentage" => $percentage,
                "date" => $date,
                "plug" => $percentage->plug_id,
                "plugs" => $plugs,
                "totalRet" => $totalRet,
            ]);
        }

        return $this->render("index", [
            "plugs" => $plugs
        ]);
    }

    /**
     * 确认分成
     * @return \yii\console\Response|\yii\web\Response
     */
    public function actionSure()
    {
        if ( !($percentage = Percentage::set($this->post("date"), $this->post("plug_id"), $this->post("upper_terminal", 0))) ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, Percentage::error());
        }
        if ( !PercentageItem::set($percentage, false) ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, PercentageItem::error());
        }
        return $this->jsonResponse(self::RESPONSE_SUCCESS, "操作成功", [
            self::RESPONSE_DATA_URL => Url::to(["index", "date_view" => $percentage->created_date, "plug_id_view" => $percentage->plug_id, "upper_terminal_view" => $percentage->upper_num])
        ]);
    }
}